//Informações separadas a serem utilizadas posteriormente na decodificação do áudio.


#include <stdio.h>
#include <stdlib.h>
#include "bitstream.h"
#include "sideinf.h"


void   inform(FILE *fp, unread *bits, int ch, sideinfo *data ) {
	
	//Offset negativo do Frame
	data->main_data = get_bit(fp, bits,9);
	
	if (ch==1) {
		data->private_bits = get_bit(fp, bits, 5);
		
		data->scfsi[0] = get_bit(fp, bits, 4);
		data->scfsi[1] = 0;
		}
	
	else {
		data->private_bits = get_bit(fp, bits, 3);	
		data->scfsi[0] = get_bit(fp, bits, 4);
		data->scfsi[1] = get_bit(fp, bits, 4);
	}


}		


void sd_gran (FILE *fp, unread *bits, int ch, granulo_info *gran ) {
	
	if (ch==1) {
		gran->can1-> part2_3_length=get_bit(fp, bits, 12);				
	
		gran->can1-> big_values   = get_bit(fp, bits, 9);
		
		gran->can1->  global_gain  =get_bit(fp, bits, 8);	
		
		gran->can1->  scalefac_compress  =get_bit(fp, bits, 4);	

		gran->can1->  window_switching  =get_bit(fp, bits, 1);	
		
		if (!(gran->can1->  window_switching  )) {
			for (int i=0;i<3;i++)			
				gran->can1->  table_select[i]=get_bit(fp, bits, 5);	
						
			gran->can1->  region0_count  =get_bit(fp, bits, 4);					
			
			gran->can1->  region1_count  =get_bit(fp, bits, 3);					

	} 

		else {
			gran->can1->  block_type  =get_bit(fp, bits, 2);

			gran->can1->  mixed_block_flag  =get_bit(fp, bits, 1); 			

			for (int i=0;i<2;i++)			
				gran->can1->  two_regions[i]=get_bit(fp, bits, 5);	
	
			for (int i=0;i<2;i++)			
				gran->can1->  subblock_gains[i]=get_bit(fp, bits,5); 

		}
	
		
	}
	
	else {
		gran->can1->  part2_3_length  =get_bit(fp, bits, 12);			
		gran->can2->  part2_3_length =get_bit(fp, bits, 12);			
			
		gran->can1->  big_values   = get_bit(fp, bits, 9);
		gran->can2->  big_values  = get_bit(fp, bits, 9);
		
		gran->can1->  global_gain  =get_bit(fp, bits, 8);	
		gran->can2->  global_gain =get_bit(fp, bits, 8);
		
		gran->can1->  scalefac_compress  =get_bit(fp, bits, 4);	
		gran->can2->  scalefac_compress =get_bit(fp, bits, 4);

		gran->can1->  window_switching  =get_bit(fp, bits, 1);	
		gran->can2->  window_switching =get_bit(fp, bits, 1);
		
		if (!(gran->can1->  window_switching  ||gran->can2->  window_switching )) {
			for (int i=0;i<3;i++)			
				gran->can1->  table_select[i]=get_bit(fp, bits, 5);	
			for (int i=3;i<6;i++)
				gran->can2->  table_select[i]=get_bit(fp, bits, 5);	
						
			gran->can1->  region0_count  =get_bit(fp, bits, 4);					
			gran->can2->  region0_count =get_bit(fp, bits, 4);
			
			gran->can1->  region1_count  =get_bit(fp, bits, 3);					
			gran->can2->  region1_count =get_bit(fp, bits, 3);
		}

		else { 

			gran->can1->  block_type  =get_bit(fp, bits, 2);
			gran->can2->  block_type =get_bit(fp, bits, 2);

			gran->can1->  mixed_block_flag  =get_bit(fp, bits, 1); 			
            gran->can2->  mixed_block_flag =get_bit(fp, bits, 1); 
			
			for (int i=0;i<2;i++)			
				gran->can1->  two_regions[i]=get_bit(fp, bits, 5);	
			for (int i=0;i<4;i++)
				gran->can2->  two_regions[i]=get_bit(fp, bits, 5);
	
			for (int i=0;i<3;i++)			
				gran->can1->  subblock_gains[i]=get_bit(fp, bits,5); 
			for (int i=0;i<6;i++)			
				gran->can2->  subblock_gains[i]=get_bit(fp, bits,5); 
		}


			
	}
	
}	

void   pos_block(FILE *fp, unread *bits, int ch, sideinfo *data ) {

	
	if (ch==1){
		data->preflag=get_bit(fp, bits, 1);			
		data->scalefac_scale =get_bit(fp, bits, 1);	
		data->count1table_select=get_bit(fp, bits, 1);
	}
	else {
		data->preflag=get_bit(fp, bits, 2);	
		data->scalefac_scale =get_bit(fp, bits, 2);	
		data->count1table_select=get_bit(fp, bits, 2);

	}

}





